package com.sita.tboard.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.mikhaellopez.circularimageview.CircularImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.sita.bike.R;
import com.sita.bike.beans.PersonBean;
import com.sita.bike.rest.model.Notes;
import com.sita.bike.rest.model.Person;
import com.sita.bike.utils.AccountUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhangdepeng on 2016/1/26.
 */
public class DynamicDetailAdapter extends BaseAdapter {
    public Context mContext;
    public List<Notes> list = new ArrayList<Notes>();
    public PersonBean personBean;
    Person mPerson;
    @Bind(R.id.senddate)
    TextView tv_date;
    public DynamicDetailAdapter(Context context) {
        this.mContext = context;
    }

    public void setList(List<Notes> list){
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.dynamic_detail_listitem, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ButterKnife.bind(this,convertView);
        if (mPerson == null) {
            mPerson = new Person();
        }

        if(list.get(position).mPerson.mNickName==null){
            viewHolder.headerImage.setImageResource(R.drawable.default_head);
        }else{
            ImageLoader.getInstance().displayImage(list.get(position).mPerson.mAvatar,
                    viewHolder.headerImage);
        }

        if(AccountUtils.getNickName()==null){
            viewHolder.tv_nickname.setText(personBean.person.getMobile());
        }else{
            viewHolder.tv_nickname.setText(AccountUtils.getUserDisplayName());
        }
        viewHolder.tv_content.setText(list.get(position).mMessage);
        changeDate(list.get(position).mCreateTime,tv_date);
        return convertView;
    }

    private class ViewHolder {
        CircularImageView headerImage;
        TextView tv_nickname;
        TextView tv_content;
        private View mParentView;

        public ViewHolder(View parentView) {
            mParentView = parentView;
            headerImage = (CircularImageView) mParentView.findViewById(R.id.head);
            tv_content = (TextView) mParentView.findViewById(R.id.content);
            tv_nickname = (TextView) mParentView.findViewById(R.id.user_nickname);
        }
    }

    public void changeDate(long ms,TextView view){
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;
        long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        if(minute<1){
            view.setText(""+second);
        }else if(hour<1){
            view.setText(""+minute);
        }else if(day<24){
            SimpleDateFormat sdf=new SimpleDateFormat("HH:ss:mm");
            Date date=new Date(ms);
            view.setText("" + sdf.format(date));
        }
    }

}
